using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

namespace UnitTesting.Fixturing
{
	class Constructors : IConstructors
	{
		readonly MethodVisibilityChecker _methodVisibilityChecker;

		[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		internal Visibilities Visibilities { get; private set; }

		internal Constructors() : this(Visibilities.All)
		{
		}

		internal Constructors(Visibilities visibilities)
		{
			_methodVisibilityChecker = new MethodVisibilityChecker(visibilities);
			Visibilities = visibilities;
		}

		public ConstructorInfo[] GetFor(Type type)
		{
			Ensure.IsNotNull(() => type);
			return GetConstructors(type)
				.Where(x => _methodVisibilityChecker.IsSatisfyVisible(x)).ToArray();
		}

		static IEnumerable<ConstructorInfo> GetConstructors(Type type)
		{
			return type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
		}
	}
}